package edu.gxmzu.task.filter;

import edu.gxmzu.task.config.security.MyUserDetails;
import edu.gxmzu.task.utils.ConvertMyUserDetailsUtil;
import edu.gxmzu.task.utils.JWTUtil;
import edu.gxmzu.task.utils.RedisService;
import jakarta.annotation.Resource;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;
import java.util.Objects;

@Component
@Slf4j
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {

    @Resource
    private RedisService redisService;
    public static final String TOKEN_KEY = "jwtToken";

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        // 获取token
        log.info("拦截请求");
        String token = request.getHeader(TOKEN_KEY);
        log.info("token: " + token);
        if (!Objects.isNull(token)) {
            // 解析token
            String username = JWTUtil.getUserNameFromToken(token);
            if (username == null) {
//                UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken("test", "test");
//                SecurityContextHolder.getContext().setAuthentication(authenticationToken);
                request.setAttribute("errorMessage","非法token!");
                throw new BadCredentialsException("非法token!");
            }
            Object myUserDetailsObject = redisService.getValue(RedisService.USER_KEY + username);
            if (Objects.isNull(myUserDetailsObject)) {
                request.setAttribute("errorMessage","token过期!");
                throw new BadCredentialsException("token过期!");
            }
            log.info("缓存值:" + myUserDetailsObject);
            MyUserDetails myUserDetails = ConvertMyUserDetailsUtil.convert(myUserDetailsObject);
            if (!JWTUtil.validateToken(token, myUserDetails)) {
                request.setAttribute("errorMessage","token无效!");
                throw new BadCredentialsException("token无效!");
            }
            // 存入SecurityContextHolder
            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(myUserDetails.getAccount(), null, myUserDetails.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
            log.info("认证成功");
        }
        // 放行
        log.info("放行");
        filterChain.doFilter(request, response);
    }
}
